package com.jushu.storbox.util;

import java.io.File;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.entities.DownloadObject;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.util.image.ImageUtils;
import com.huawei.sharedrive.sdk.android.util.StringUtil;

public class DirectoryUtil {

	private static final String TAG = DirectoryUtil.class.getSimpleName();
	
	public static void saveCachePath(Context context,String cachePath){
		SharedPreferences settings = context.getSharedPreferences(
				ClientConfig.settings, Context.MODE_APPEND);
		Editor editor = settings.edit();
		editor.putString(ClientConfig.CURRENT_CACHE_PATH, cachePath);
		editor.commit();	
	}
	
	public static String getCurrentCachePath(Context context){
		String cachePath = null;
		try {
			SharedPreferences settings = context.getSharedPreferences(
					ClientConfig.settings, Context.MODE_APPEND);
			cachePath = settings.getString(ClientConfig.CURRENT_CACHE_PATH, "");
			File cachePathFile = new File(cachePath);
			if(StringUtil.isBlank(cachePath) || !cachePathFile.exists() 
					|| !cachePathFile.isDirectory() || cachePathFile.listFiles() == null || cachePathFile.listFiles().length <= 0){
				cachePath = ClientConfig.CACHE_DEFAULT_PATH;
				Editor editor = settings.edit();
				editor.putString(ClientConfig.CURRENT_CACHE_PATH, cachePath);
				editor.commit();
			}
		} catch (Exception e) {
		}
		return cachePath;
	}
	
	/**
	 * Rule:/CloudDrive/download/{username}/tempdownload/ownerId_iNodeId_fileName
	 */
	public static String generateTempShareDownloadPath(Context context,String ownedBy, String iNodeId, String fileName){
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentCachePath(context));
		sb.append(ClientConfig.CACHE_CONTEXT);
		sb.append(PublicTools.getClientUserName(context));
		sb.append(ClientConfig.TEMPDOWNLOAD);
		sb.append(generateFileName(ownedBy, iNodeId, fileName));
		return sb.toString();
	}
	
	public static String generateFileName(String ownedBy, String resourceId, String fileName) {
		StringBuffer targetBuffer = new StringBuffer();
		targetBuffer.append(ownedBy).append("_").append(resourceId);
		if(fileName.indexOf(".") != -1 && !fileName.endsWith(".")){
			targetBuffer.append(fileName.substring(fileName.lastIndexOf("."),fileName.length()));
		}
		return targetBuffer.toString();
	}

	/**
	 * rule:/CloudDrive/data/{username}/sharedownload/
	 */
	public static String generateShareDir(Context context){
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentCachePath(context));
		sb.append(ClientConfig.CACHE_CONTEXT);
		sb.append(PublicTools.getClientUserName(context)).append(ClientConfig.SHAREDOWNLOAD);
		return sb.toString();
	}

	/**
	 * rule:/CloudDrive/download/{username}/tempdownload/ownerId_fileId_fileName
	 */
	public static String generateTempFileDownloadPath(Context context,String fileId, String fileName){
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentCachePath(context));
		sb.append(ClientConfig.CACHE_CONTEXT);
		sb.append(PublicTools.getClientUserName(context));
		sb.append(ClientConfig.TEMPDOWNLOAD);
		sb.append(generateFileName(PublicTools.getOwnerId(context), fileId, fileName));
		return sb.toString();
	}

	/**
	 * rule:/CloudDrive/data/{username}/sharedownload/ownerId_fileId_fileName
	 */
	public static String generateShareDownloadPath(Context context,long ownedBy, long iNodeId, String fileName){
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentCachePath(context));
		sb.append(ClientConfig.CACHE_CONTEXT);
		sb.append(PublicTools.getClientUserName(context));
		sb.append(ClientConfig.SHAREDOWNLOAD);
		sb.append(ownedBy).append("_").append(iNodeId).append("_").append(fileName);
		return sb.toString();
	}

	/**
	 * rule:/CloudDrive/data/{username}/sharedownload/ownerId_fileId_fileName
	 */
	public static String generateShareDownloadPath(Context context,String ownedBy, String iNodeId, String fileName){
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentCachePath(context));
		sb.append(ClientConfig.CACHE_CONTEXT);
		sb.append(PublicTools.getClientUserName(context));
		sb.append(ClientConfig.SHAREDOWNLOAD);
		sb.append(ownedBy).append("_").append(iNodeId).append("_").append(fileName);
		return sb.toString();
	}

	public static String generateLocalFileThumbnailPath(Context context, String fileName){
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentThumbnailPath(context)).append(fileName);
		return sb.toString();
	}
	
	public static String getCurrentThumbnailPath(Context context){
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentCachePath(context));
		sb.append(ClientConfig.CACHE_CONTEXT);
		sb.append(PublicTools.getClientUserName(context));
		sb.append(ClientConfig.MY_THUMBNAIL);
		return sb.toString();
	}
	
	public static String getCurrentSharedThumbnailPath(Context context){
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentCachePath(context));
		sb.append(ClientConfig.CACHE_CONTEXT);
		sb.append(PublicTools.getClientUserName(context));
		sb.append(ClientConfig.SHARED_THUMBNAIL);
		return sb.toString();
	}

	/**
	 * rule:/CloudDrive/data/{username}/
	 */
	public static String generateOwnerDir(Context context){
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentCachePath(context));
		sb.append(ClientConfig.CACHE_CONTEXT);
		sb.append(PublicTools.getClientUserName(context)).append(File.separator);
		return sb.toString();
	}

	/**
	 * rule:/CloudDrive/data/{username}/mydownload/
	 */
	public static String generateDownloadDir(Context context){
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentCachePath(context));
		sb.append(ClientConfig.CACHE_CONTEXT);
		sb.append(PublicTools.getClientUserName(context)).append(ClientConfig.MYDOWNLOAD);
		return sb.toString();
	}
	
	public static String generateTempDir(Context context){
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentCachePath(context));
		sb.append(ClientConfig.CACHE_CONTEXT);
		sb.append(PublicTools.getClientUserName(context));
		sb.append(ClientConfig.TEMPDOWNLOAD);
		return sb.toString();
	}

    public static String genThumbnailFileFullName(Context context,String ownerId, String fileId, String fileName){
    	StringBuffer sb = new StringBuffer();
    	sb.append(getCurrentThumbnailPath(context));
    	sb.append(ownerId);
    	sb.append(Constant.FILE_NAME_SEPERATE);
    	sb.append(fileId);
    	sb.append(Constant.FILE_NAME_SEPERATE);
    	sb.append(fileName);
    	return sb.toString();
    }
    public static String genThumbnailFileFullName(Context context, FileFolderInfo info){
    	return genThumbnailFileFullName(context, info.getOwnerBy(), info.getId(), info.getName());
    }
    
    public static String genSharedThumbnailFullName(Context context, String fileId, String fileName){
    	StringBuffer sb = new StringBuffer();
    	sb.append(getCurrentSharedThumbnailPath(context));
    	sb.append(PublicTools.getOwnerId(context));
    	sb.append(Constant.FILE_NAME_SEPERATE);
    	sb.append(fileId);
    	sb.append(Constant.FILE_NAME_SEPERATE);
    	sb.append(fileName);
    	return sb.toString();
    }

	/**
	 * rule:/CloudDrive/data/{username}/mydownload/ownerId_fileId_fileName
	 */
	public static String generateFileDownloadPath(Context context, String ownerId, String fileId, String fileName) {
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentCachePath(context));
		sb.append(ClientConfig.CACHE_CONTEXT);
		sb.append(PublicTools.getClientUserName(context)).append(ClientConfig.MYDOWNLOAD);
		sb.append(generateFileName(ownerId, fileId, fileName));
		return sb.toString();
	}
	
	/**
	 * rule:/CloudDrive/data/{username}/mydownload/ownerId_fileId_fileName
	 */
	public static String generateFileDownloadPath(Context context, DownloadObject downItem) {
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentCachePath(context));
		sb.append(ClientConfig.CACHE_CONTEXT);
		sb.append(PublicTools.getClientUserName(context)).append(ClientConfig.MYDOWNLOAD);
		sb.append(generateFileName(downItem.getOwnerBy(), downItem.getId(), downItem.getFileName()));
		return sb.toString();
	}
	
	/**
	 * rule:/CloudDrive/data/{username}/mydownload/ownerId_fileId_fileName
	 */
	public static String generateFileDownloadPath(Context context, FileFolderInfo fileInfo) {
		StringBuffer sb = new StringBuffer();
		sb.append(getCurrentCachePath(context));
		sb.append(ClientConfig.CACHE_CONTEXT);
		sb.append(PublicTools.getClientUserName(context)).append(ClientConfig.MYDOWNLOAD);
		sb.append(generateFileName(fileInfo.getOwnerBy(), fileInfo.getId(), fileInfo.getName()));
		return sb.toString();
	}

	public static void deleteFileThumbnailPath(Context context, String OwnerBy,  String fileId, String fileName){
		if (ImageUtils.isImageType(fileName)){
			String filePath = genThumbnailFileFullName(context, OwnerBy,fileId,fileName);
			File file = new File(filePath);
			if(file.exists()) {
				file.delete();
			}
		} else {
			LogUtil.e(TAG, "rename item is not imageType");
		}
	}
	
	/**
	 * @param context
	 * @param fileId
	 * @param fileName
	 * @param newFileName
	 */
	public static void renameThumbnailFile(Context context, String OwnerBy, String fileId, String fileName, String newFileName){
		if (ImageUtils.isImageType(fileName)){
			String srcFileName = genThumbnailFileFullName(context,OwnerBy, fileId,fileName);  
			if(new File(srcFileName).exists()){
				String newTargetFileName = genThumbnailFileFullName(context,OwnerBy,fileId,newFileName); 
				FileUtil.renameFile(srcFileName, newTargetFileName);
			}
		} else {
			LogUtil.e(TAG, "rename item is not imageType");
		}
		
	}

	public static String generateFileName(Context context, String fileId, String fileName){
		StringBuffer sb = new StringBuffer();
		sb.append(PublicTools.getOwnerId(context)).append("_").append(fileId).append("_").append(fileName);
		return sb.toString();
	}

	public static void deleteFile(Context context,
			FileFolderInfo fileFolderInfomation) {
		String path = generateFileDownloadPath(context, fileFolderInfomation.getOwnerId(),fileFolderInfomation.getId(), fileFolderInfomation.getName());
		File file = new File(path);
		if(file.exists()) {
			file.delete();
		}
	}

}
